import { defineStore } from 'pinia'
import type { Comment } from '../../types/JSJL/CommentStore'

export const useCommentStore = defineStore('teachingComment', {

    // 持久化配置
    persist: {
        storage: localStorage,
        paths: ['comments', "replyingTo"]
    },
    state: () => ({
        comments: [] as Comment[], // 评论列表
        loading: false, // 加载状态
        error: null as string | null, // 错误信息
        replyingTo: null as number | null // 当前正在回复的评论ID
    }),

    actions: {
        // 获取帖子的评论
        async fetchComments(postId: number) {
            this.loading = true
            try {
                const response = await getCommentsByPostIdApi(postId)
                this.comments = this.transformComments(response.data)
            } catch (error) {
                this.error = error.message
            } finally {
                this.loading = false
            }
        },

        // 转换评论为树形结构
        transformComments(comments: Comment[]): Comment[] {
            const commentMap = new Map<number, Comment>()
            const rootComments: Comment[] = []

            // 创建映射
            comments.forEach(comment => {
                commentMap.set(comment.id, { ...comment, children: [] })
            })

            // 构建树形结构
            comments.forEach(comment => {
                const node = commentMap.get(comment.id)
                if (comment.parentId === 0) {
                    rootComments.push(node!)
                } else {
                    const parent = commentMap.get(comment.parentId)
                    parent?.children?.push(node!)
                }
            })

            return rootComments
        },

        // 添加评论
        async addComment(commentData: {
            postId: number
            content: string
            parentId?: number
            replyToUserId?: number
        }) {
            this.loading = true
            try {
                const response = await addCommentApi(commentData)
                // 重新加载评论
                await this.fetchComments(commentData.postId)
                return response.data
            } catch (error) {
                this.error = error.message
                throw error
            } finally {
                this.loading = false
                this.replyingTo = null
            }
        },

        // 删除评论
        async deleteComment(commentId: number, postId: number) {
            this.loading = true
            try {
                await deleteCommentApi(commentId)
                // 重新加载评论
                await this.fetchComments(postId)
            } catch (error) {
                this.error = error.message
                throw error
            } finally {
                this.loading = false
            }
        },

        // 设置当前回复的评论
        setReplyingTo(commentId: number | null) {
            this.replyingTo = commentId
        }
    },

    getters: {
        // 获取主评论(非回复)
        mainComments: (state) => {
            return state.comments.filter(comment => comment.parentId === 0)
        },

        // 获取指定评论的回复
        repliesToComment: (state) => (commentId: number) => {
            const comment = state.comments.find(c => c.id === commentId)
            return comment?.children || []
        }
    }
})
